package homework;

public class Racer extends Thread{
    private long meterTime;
    private long restTime;
    private long time;



    //有参构造
    public Racer(String name, long meterTime, long restTime) {
        super(name);
        this.meterTime = meterTime;
        this.restTime = restTime;
    }

    public void run(){
        long start = System.currentTimeMillis();
        for (int i=1; i<=30; i++){//i单位是米
            try {
                Thread.sleep(meterTime);
                System.out.println(getName() + "已经跑了" + i + "米");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                if(i==10 || i==20){
                    System.out.println(getName() +"正在休息....");
                    Thread.sleep(restTime);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(getName() + "到达终点.");
        long end = System.currentTimeMillis();
        time =  end - start;
    }

    public long getTime() {
        return time;
    }
}

